package cn.da.shuai.cool.file.api.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.file.model.CoolFileVo;
import cn.da.shuai.cool.file.service.service.IFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@RequestMapping("/file")
@Tag(name = "文件管理")
public class FileController {

    @Autowired
    private IFileService fileService;

    @Operation(summary = "上传文件")
    @PostMapping("/upload")
    public R<String> uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
        CoolFileVo coolFileVo = new CoolFileVo(
                file.getInputStream(),
                file.getOriginalFilename(),
                file.getContentType(),
                file.getSize()
        );
        String fileName = fileService.uploadFile(coolFileVo);
        return R.ok(fileName);
    }

    @Operation(summary = "上传图片")
    @PostMapping("/upload/image")
    public R<String> uploadImage(@RequestParam("file") MultipartFile file) throws Exception {
        CoolFileVo coolFileVo = new CoolFileVo(
                file.getInputStream(),
                file.getOriginalFilename(),
                file.getContentType(),
                file.getSize()
        );
        String fileName = fileService.uploadImage(coolFileVo);
        return R.ok(fileName);
    }

    @Operation(summary =  "下载文件")
    @GetMapping("/download/{fileName}")
    public ResponseEntity<InputStreamResource> downloadFile(@PathVariable String fileName) throws Exception {
        InputStream inputStream = fileService.downloadFile(fileName);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachment", URLEncoder.encode(fileName, StandardCharsets.UTF_8));

        return ResponseEntity.ok()
                .headers(headers)
                .body(new InputStreamResource(inputStream));
    }

    @Operation(summary = "删除文件")
    @DeleteMapping("/{fileName}")
    public R<Void> deleteFile(@PathVariable String fileName) throws Exception {
        fileService.deleteFile(fileName);
        return R.ok();
    }

    @Operation(summary = "列出文件列表")
    @GetMapping("/list")
    public R<List<String>> listFiles() throws Exception {
        return R.ok(fileService.listFiles());
    }

    @Operation(summary = "查看文件")
    @GetMapping("/view/{fileName}")
    public R<String> viewFile(@PathVariable String fileName) throws Exception {
        return R.ok(fileService.getPresignedUrl(fileName));
    }
}